package leetcode.code0049;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution1 extends Solution49 {

	@Override
	public List<List<String>> groupAnagrams(String[] strs) {
		List<List<String>> ans = new ArrayList<>();
		if (strs == null || strs.length == 0)
			return ans;
		Map<String, List<String>> map = new HashMap<>();
		for (String str : strs) {
			String key = this.key(str);
			List<String> list = map.getOrDefault(key, new ArrayList<String>());
			list.add(str);
			map.put(key, list);
		}
		for (String key : map.keySet()) {
			ans.add(map.get(key));
		}
		return ans;
	}

	private String key(String str) {
		StringBuffer ans = new StringBuffer();
		int[] dic = new int[26];
		for (char c : str.toCharArray()) {
			dic[c - 'a']++;
		}
		for (int n : dic) {
			ans.append(n).append('-');
		}
		return ans.toString();
	}

	public static void main(String[] args) {
		Solution1 so = new Solution1();
		so.debug1();
		so.debug2();
		so.debug3();
	}

}
